﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataLib.Accounts.Bus;
using System.Data;

namespace Ewts.Storage
{
    public partial class outStorageUpdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Context.Response.Cache.SetCacheability(HttpCacheability.NoCache);       //清理缓存，保证刷新页面数据是最新的
            if (!Page.IsPostBack)
            {
                //归属地
                ListItem item = new ListItem();
                item.Text = "--请选择--";
                item.Value = "";
                DataLib.BLL.City bllLib = new DataLib.BLL.City();
                dropBelong.DataSource = bllLib.GetList("flag1='01'");
                dropBelong.DataTextField = "cityname";
                dropBelong.DataValueField = "cityname";
                dropBelong.DataBind();
                dropBelong.Items.Insert(0, item);



                // 处理地
                dropGraphiccity.DataSource = bllLib.GetList("flag1='02'");
                dropGraphiccity.DataTextField = "cityname";
                dropGraphiccity.DataValueField = "cityname";
                dropGraphiccity.DataBind();
                dropGraphiccity.Items.Insert(0, item);

                bindMonth();
                User curUser = new User();
                curUser = (User)Session["UserInfo"];
                hidOper.Value = curUser.UserName;
                dropBelong.SelectedValue = curUser.CompanyCity;
                dropGraphiccity.SelectedValue = curUser.CompanyCity;

                if (Request["outid"] != null && Request["outid"].ToString().Trim() != "")
                {
                    hidoutid.Value = Request["outid"].ToString().Trim();
                    DataLib.Model.OutStorage model = new DataLib.BLL.OutStorage().GetModel(Convert.ToInt32(hidoutid.Value));
                    oldload.Value = model.VLoadNum;
                    txtConsumMan.Value = model.ConsumMan;
                    Beglong_old.Value = model.inDepart;
                    Gropphic_old.Value = model.OutDepart;
                    dropBelong.SelectedValue = model.belogarea;
                    dropGraphiccity.SelectedValue = model.graphicCity;
                    txrRemark.Value = model.memo;
                    string stroutype = "";
                    switch (model.outStorageType) { 
                        case "1":
                            stroutype = "拆解出库";
                            break;
                        case "2":
                            stroutype = "分类出库";
                            break;
                        case "3":
                            stroutype = "生产出库";
                            break;
                        case "4":
                            stroutype = "销售出库";
                            break;
                        case "5":
                            stroutype = "移库出库";
                            break;
                        default:
                            break;
                    }
                    h5_outno.InnerText = stroutype + " " + model.outStorageNo;
                    hidoutType.Value = model.outStorageType;
                    //txtOutStorageCode.Value = model.outStorageNo;

                }
            }
            bindData(hidoutid.Value.Trim());
        }

        /// <summary>
        /// 获取客户编码或者客户名称
        /// </summary>
        /// <param name="barid">物品ID号</param>
        /// <param name="flag">0是编码</param>
        /// <returns></returns>
        protected string GetCustomerCode(string barid, int flag)
        {
            string str = "";
            DataLib.Model.BarCode bModel = new DataLib.BLL.BarCode().GetModel(int.Parse(barid));
            if (bModel.loadnumid != 0)
            {
                DataLib.Model.LoadNum loadModel = new DataLib.BLL.LoadNum().GetModel((int)bModel.loadnumid);
                if (loadModel != null)
                {
                    DataLib.Model.Cumtomer cumModel = new DataLib.BLL.Cumtomer().GetModel((int)loadModel.customerid);
                    if (cumModel != null)
                    {
                        if (flag == 0)
                            str = cumModel.customercode;
                        else
                            str = cumModel.companyname;
                    }
                }
            }
            return str;
        }

        /// <summary>
        /// 绑定出库明细
        /// </summary>
        /// <param name="outid"></param>
        protected void bindData(string outid)
        {
            DataLib.BLL.OutStorage bllOuts = new DataLib.BLL.OutStorage();
            DataLib.Model.OutStorage modelOuts = new DataLib.Model.OutStorage();
            if (outid != "0" && outid != null && outid != "")
            {
                modelOuts = bllOuts.GetModel(int.Parse(outid));
                if (modelOuts.clearmonth != null && modelOuts.clearmonth != "")
                {
                    string strYear = modelOuts.clearmonth.Substring(0, 4);
                    string strMonth = modelOuts.clearmonth.Substring(4, 2);
                    dropYear.SelectedValue = strYear;
                    dropMonth.SelectedValue = strMonth;
                }
            }

            if (outid != null && outid.Trim() != "0")
            {
                DataTable dt = new DataLib.BLL.BarCode().GetList("isdel_out!=-1 and OutStrorageID=" + outid).Tables[0];
                if (dt != null)
                {
                    rept.DataSource = dt;
                    rept.DataBind();
                }
            }
        }

        /// <summary>
        /// 绑定财务结算月
        /// </summary>
        protected void bindMonth()
        {
            int year = DateTime.Now.Year;

            ListItem listitem = new ListItem((year + 1).ToString() + "年", (year + 1).ToString());
            dropYear.Items.Add(listitem);
            listitem = new ListItem(year.ToString() + "年", year.ToString());
            dropYear.Items.Add(listitem);
            listitem = new ListItem((year - 1).ToString() + "年", (year - 1).ToString());
            dropYear.Items.Add(listitem);

            dropYear.SelectedValue = year.ToString();
            dropMonth.SelectedValue = DateTime.Now.Month.ToString().PadLeft(2, '0');

        }
    }
}